package com.chat.net.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.logging.Handler;

import com.chat.net.socket.ObjectChannelContext;

public class SocketChannelManager {
    Map<String,Channel> channelMap = new HashMap<>();
//    List<ChannelHandler> handlers = new ArrayList<>();
    ServerSocket serverSocket;
    HandlerManager handlerManager;
    int port;
    ManagerStatue statue;

    public enum ManagerStatue{
        INIT,
        RUN,
        DAMAGE
    }

    public SocketChannelManager(int port) {
        this.port = port;
        handlerManager = new HandlerManager();
//        init();
    }

    public void init(){
        statue = ManagerStatue.INIT;
        try {
            serverSocket = new ServerSocket(port);
            statue = ManagerStatue.RUN;
            while(statue == ManagerStatue.RUN){
                Socket socket = serverSocket.accept();
                System.out.println("接收连接请求");
                ObjectChannelContext socketChannelContext = new ObjectChannelContext(socket);
                new Thread(socketChannelContext).start();
                SocketChannel socketChannel = new SocketChannel(socketChannelContext);
                channelMap.put(socket.getInetAddress().getHostAddress(),socketChannel);
            }
        } catch (IOException e) {
            statue = ManagerStatue.DAMAGE;
            System.out.println("服务器监听异常");
            e.printStackTrace();
        }
    }



    public void handler() {
        //read
        Iterator iterator = channelMap.keySet().iterator();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            Channel channel = channelMap.get(key);
            try {
                handlerManager.handler(channel);
            } catch (IOException e) {
                iterator.remove();
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
//            try {
//                ObjectChannelContext objectChannelContext = (ObjectChannelContext) channel.getChannelContext();
//                System.out.println("handler数据");
//                Object object = objectChannelContext.readObject();
//                for(int i = 0 ; i < handlers.size() ; i++){
//                    ChannelHandler handler = handlers.get(i);
//                    handler.handler(channel,object);
//                }
//            } catch (IOException e) {
//                iterator.remove();
//                e.printStackTrace();
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
        }
    }

    public void destory(){
        statue = ManagerStatue.DAMAGE;
        channelMap.values().stream()
                .forEach(channel -> {
                    try {
                        channel.getChannelContext().getSocket().close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("服务关闭!");
    }

//    @Override
//    public void addHandler(ChannelHandler channelHandler) {
//        handlers.add(channelHandler);
//    }

    public ManagerStatue getStatue() {
        return statue;
    }

    public HandlerManager getHandlerManager() {
        return handlerManager;
    }
}
